package com.example.asus.map;

import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AMap.OnMapLongClickListener,PoiSearch.OnPoiSearchListener {
  private MapView mapView;
    private AMap aMp;
    MyLocationStyle myLocationStyle;
    private EditText edtKey;
    private Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView=findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        aMp=mapView.getMap();
        initStyle();
        initSearch();
    }

    private void initSearch() {
        edtKey=findViewById(R.id.edt_key);
        btnSearch=findViewById(R.id.btn_search);
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String key=edtKey.getText().toString();
                PoiSearch.Query query=new PoiSearch.Query(key,"","020");
                query.setPageSize(10);//设置每页最多返回多少条poiltem
                query.setPageNum(0);//设置查询页码

                PoiSearch poiSearch=new PoiSearch(MainActivity.this,query);
                    poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(23.18321,113.353111), 2000));
                poiSearch.setOnPoiSearchListener(MainActivity.this);
               poiSearch.searchPOIAsyn();
            }
        });
    }

    private void initStyle() {
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(3000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        aMp.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMp.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示，非必需设置。
        //aMp.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        //aMp.showIndoorMap(true); //true：显示室内地图；false：不显示；
        //aMp.setMapType(AMap.MAP_TYPE_SATELLITE);// 设置卫星地图模式，aMap是地图控制器对象
        //aMp.setMapType(AMap.MAP_TYPE_NIGHT);//夜景地图，aMap是地图控制器对象。
       //aMp.setTrafficEnabled(true);//显示实时路况图层，aMap是地图控制器对象。
       //public void setMapLanguage(AMap.ENGLISH);
        CameraUpdate cameraUpdateFactory= CameraUpdateFactory.newCameraPosition
                (new CameraPosition(new LatLng(23.18321,113.353111),15,30,0 ));
        aMp.moveCamera(cameraUpdateFactory);
        aMp.setOnMapLongClickListener(this);

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }


    public void onMyLocationChange(Location location) {

    }

    @Override
    public void onMapLongClick(LatLng latLng) {

    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
      Log.e("MainActivity","onPoiSearched");
      if(poiResult==null){
          return;
      }
        ArrayList<PoiItem> poiItemArrayList=poiResult.getPois();
      for(PoiItem poiItem: poiItemArrayList){
          Log.e("MainActivity",poiItem.getTitle());
          double latitude=poiItem.getLatLonPoint().getLatitude();
          double longitude=poiItem.getLatLonPoint().getLongitude();

          LatLng latLng=new LatLng(latitude,longitude);
          aMp.addMarker(new MarkerOptions().position(latLng).title(poiItem.getTitle()).snippet(poiItem.getTel()));

      }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
